package org.example.wechat.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.wechat.model.Friend;
import org.example.wechat.model.HeadPortrait;
import org.example.wechat.model.User;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;

@Mapper

public interface UserMapper {
    @Select("select * from user where username = #{username}")
    User selectByName(String username);//通过账号查询用户账号信息

    List<User> selectUserByUsername(User user);//通过用户账号搜索用户
    List<User> selectUserByName(User user);//通过用户名称搜索用户
    User selectUserByUserId(Integer id);//通过用户Id查询用户信息

    @Insert("insert into user (username,password,name,sex,province) values (#{username},#{password}, #{name},#{sex},#{province})")
    int insert(User user);//添加新用户
    @Insert("insert into friend (user_id,friend_id) values (#{userId},#{friendId})")
    int insertFriend(Friend friend);//添加新朋友


    // 查询用户头像，如果没有则给一个默认的用户头像
    @Select("select url from head_portrait where user_id = #{userId}")
    String headPortraitUrl(Integer userId);

    default String getHeadPortraitUrlOrDefault(Integer userId) {
        String url = headPortraitUrl(userId);
        System.out.println(url);
        if (url == null || url.isEmpty()) {
            System.out.println("默认头像");
            return "default_avatar.png";
        }
        return url + ".png";
    }
    // 添加用户头像
    @Insert("insert into head_portrait (user_id, url) values (#{userId}, #{url})")
    int insertHeadPortrait(HeadPortrait headPortrait);

    // 修改用户头像
    @Update("update head_portrait set url = #{url} where user_id = #{userId}")
    int updateHeadPortrait(HeadPortrait headPortrait);
}
